<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="keyword" content="">
    <meta name="theme-color" content="#600090">
    <meta name="msapplication-navbutton-color" content="#600090">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="#600090">
    <link rel="shortcut icon" href="https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-person-128.png">
    <link rel="alternate" type="application/atom+xml" title="Hexo" href="/atom.xml">
    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css">
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.css">
    <title>
        
        PageHelper初使用｜undefined
        
    </title>

    <link rel="canonical" href="https://bigbenn.gitee.io/myblog/2020/04/28/PageHelper初使用/">

    <!-- Bootstrap Core CSS -->
    
<link rel="stylesheet" href="/myblog/css/bootstrap.min.css">


    <!-- Custom CSS -->
    
<link rel="stylesheet" href="/myblog/css/blog-style.css">


    <!-- Pygments Github CSS -->
    
<link rel="stylesheet" href="/myblog/css/syntax.css">

<meta name="generator" content="Hexo 4.2.0"></head>

<style>

    header.intro-header {
        background-image: url('')
    }
</style>
<!-- hack iOS CSS :active style -->
<body ontouchstart="" class="animated fadeIn">
<!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top " id="nav-top" data-ispost = "true" data-istags="false
" data-ishome = "false" >
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand animated pulse" href="/myblog/">
                <span class="brand-logo">
                    Hexo
                </span>
                's Blog
            </a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <!-- Known Issue, found by Hux:
            <nav>'s height woule be hold on by its content.
            so, when navbar scale out, the <nav> will cover tags.
            also mask any touch event of tags, unfortunately.
        -->
        <!-- /.navbar-collapse -->
        <div id="huxblog_navbar">
            <div class="navbar-collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <a href="/myblog/">Home</a>
                    </li>
					
                    
					
					
                </ul>
            </div>
        </div>
    </div>
    <!-- /.container -->
</nav>
<script>
    // Drop Bootstarp low-performance Navbar
    // Use customize navbar with high-quality material design animation
    // in high-perf jank-free CSS3 implementation
//    var $body   = document.body;
    var $toggle = document.querySelector('.navbar-toggle');
    var $navbar = document.querySelector('#huxblog_navbar');
    var $collapse = document.querySelector('.navbar-collapse');

    $toggle.addEventListener('click', handleMagic)
    function handleMagic(e){
        if ($navbar.className.indexOf('in') > 0) {
        // CLOSE
            $navbar.className = " ";
            // wait until animation end.
            setTimeout(function(){
                // prevent frequently toggle
                if($navbar.className.indexOf('in') < 0) {
                    $collapse.style.height = "0px"
                }
            },400)
        }else{
        // OPEN
            $collapse.style.height = "auto"
            $navbar.className += " in";
        }
    }
</script>

<!-- Main Content -->

<!--only post-->


<img class="wechat-title-img"
     src="">


<style>
    
    header.intro-header {
        background-image: url('')
    }

    
</style>

<header class="intro-header">
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1 text-center">
                <div class="post-heading">
                    <h1>PageHelper初使用</h1>
                    
                    <span class="meta">
                         作者 Bigben
                        <span>
                          日期 2020-04-28
                         </span>
                    </span>
                    <div class="tags text-center">
                        
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="post-title-haojen">
        <span>
            PageHelper初使用
        </span>
    </div>
</header>

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">
            <!-- Post Container -->
            <div class="col-lg-8 col-lg-offset-1 col-sm-9 post-container">
                <h1 id="PageHelper初使用"><a href="#PageHelper初使用" class="headerlink" title="PageHelper初使用"></a>PageHelper初使用</h1><p>PageHelper很早就接触了，但是没有系统的去使用过，最近工作上（才开始工作）用到了几次，为了增强自己的理解写下这篇博客。</p>
<p>使用前准备：</p>
<p>一般使用PageHelper都是一些列表查询，用来分页，然后列表查询自然是可以是有条件的查询或者是直接全部查出来，因此需要前端至少传两个基本的分页条件到后端，准备如下方的Bean（不唯一）：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MaintainItemBean</span> </span>&#123;</span><br><span class="line">    <span class="meta">@ApiModelProperty</span>(value = <span class="string">"年度"</span>)</span><br><span class="line">    <span class="keyword">private</span> Integer year;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiModelProperty</span>(value = <span class="string">"项目名称"</span>)</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiModelProperty</span>(value = <span class="string">"开始时间"</span>)</span><br><span class="line">    <span class="keyword">private</span> String startTime;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiModelProperty</span>(value = <span class="string">"结束时间"</span>)</span><br><span class="line">    <span class="keyword">private</span> String endTime;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiModelProperty</span>(value = <span class="string">"当前页数"</span>)</span><br><span class="line">    <span class="keyword">private</span> Integer currentPage=<span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApiModelProperty</span>(value = <span class="string">"页面大小"</span>)</span><br><span class="line">    <span class="keyword">private</span> Integer pageSize=<span class="number">20</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>然后还需要准备两个实体类</p>
<p>一是页码信息Page类：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Page</span> <span class="keyword">implements</span> <span class="title">Serializable</span> </span>&#123;</span><br><span class="line">            <span class="comment">// TODO 当前页码</span></span><br><span class="line"></span><br><span class="line">            <span class="keyword">private</span> Integer currentPage = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">            <span class="comment">// TODO 每页有多少条数据</span></span><br><span class="line"></span><br><span class="line">            <span class="keyword">private</span> Integer pageSize = <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line">            <span class="comment">// TODO 总页数</span></span><br><span class="line"></span><br><span class="line">            <span class="keyword">private</span> Integer totalPage;</span><br><span class="line"></span><br><span class="line">            <span class="comment">// TODO 总条数数据</span></span><br><span class="line"></span><br><span class="line">            <span class="keyword">private</span> Long totalSize;</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>

<p>二是查询的返回类：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PageInfoListResult</span>&lt;<span class="title">T</span>&gt; </span>&#123;</span><br><span class="line"></span><br><span class="line">           <span class="keyword">private</span> List&lt;T&gt; dataList;</span><br><span class="line">           <span class="keyword">private</span> Page page;</span><br><span class="line"></span><br><span class="line">           <span class="function"><span class="keyword">public</span> List&lt;T&gt; <span class="title">getDataList</span><span class="params">()</span> </span>&#123;</span><br><span class="line">               <span class="keyword">return</span> dataList;</span><br><span class="line">           &#125;</span><br><span class="line"></span><br><span class="line">           <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setDataList</span><span class="params">(List&lt;T&gt; dataList)</span> </span>&#123;</span><br><span class="line">               <span class="keyword">this</span>.dataList = dataList;</span><br><span class="line">           &#125;</span><br><span class="line"></span><br><span class="line">           <span class="function"><span class="keyword">public</span> Page <span class="title">getPage</span><span class="params">()</span> </span>&#123;</span><br><span class="line">               <span class="keyword">return</span> page;</span><br><span class="line">           &#125;</span><br><span class="line"></span><br><span class="line">           <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setPage</span><span class="params">(Page page)</span> </span>&#123;</span><br><span class="line">               <span class="keyword">this</span>.page = page;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br></pre></td></tr></table></figure>

<p>还有一个工具类：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> &lt;T&gt; <span class="function">PageInfoListResult&lt;T&gt; <span class="title">makePageListResult</span><span class="params">(Integer currentPage, Integer pageSize, List&lt;T&gt; dataList)</span> </span>&#123;</span><br><span class="line">        PageInfoListResult&lt;T&gt; pageInfoListResult = <span class="keyword">new</span> PageInfoListResult&lt;&gt;();</span><br><span class="line">        Page page = <span class="keyword">new</span> Page(currentPage,pageSize);</span><br><span class="line">        PageInfo pageInfo = <span class="keyword">new</span> PageInfo&lt;&gt;(dataList);</span><br><span class="line">        page.setTotalPage(pageInfo.getPages());</span><br><span class="line">        page.setTotalSize(pageInfo.getTotal());</span><br><span class="line">        pageInfoListResult.setDataList(dataList);</span><br><span class="line">        pageInfoListResult.setPage(page);</span><br><span class="line">        <span class="keyword">return</span> pageInfoListResult;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>然后就能够使用了：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> PageInfoListResult <span class="title">queryList</span><span class="params">(MaintainItemBean maintainItemBean)</span> </span>&#123;</span><br><span class="line">        Integer currentPage = maintainItemBean.getCurrentPage();</span><br><span class="line">        Integer pageSize = maintainItemBean.getPageSize();</span><br><span class="line">        PageHelper.startPage(currentPage,pageSize);</span><br><span class="line">        List&lt;MaintainItem&gt; maintainItems = <span class="keyword">this</span>.maintainItemMapper.queryMaintainItemList(maintainItemBean);<span class="comment">//Mybatis基本查询</span></span><br><span class="line">        PageInfoListResult result = GeneralUtil.makePageListResult(currentPage, pageSize, maintainItems);</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>返回给前端的是一个dataList集合以及一个Page对象。</p>

                <hr>
                

                <ul class="pager">
                    
                    <li class="previous">
                        <a href="/myblog/2020/05/09/Java实体类和JSON之间的转换/" data-toggle="tooltip" data-placement="top"
                           title="Java实体类和JSON之间的转换">&larr; Previous Post</a>
                    </li>
                    
                    
                    <li class="next">
                        <a href="/myblog/2020/04/28/关于Session的使用/" data-toggle="tooltip" data-placement="top"
                           title="关于Session的使用">Next Post &rarr;</a>
                    </li>
                    
                </ul>

                

                


                <!--加入新的评论系统-->
                

                

            </div>

            <div class="hidden-xs col-sm-3 toc-col">
                <div class="toc-wrap">
                    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#PageHelper初使用"><span class="toc-text">PageHelper初使用</span></a></li></ol>
                </div>
            </div>
        </div>

        <div class="row">
            <!-- Sidebar Container -->

            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                sidebar-container">

                <!-- Featured Tags -->
                

                <!-- Friends Blog -->
                
            </div>
        </div>

    </div>
</article>







<!-- Footer -->
<!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1 text-center">
                <br>
                <ul class="list-inline text-center">
                
                
                

                

                

                

                

                </ul>
                <p class="copyright text-muted">
                    Copyright &copy; Hexo 2020
                    <br>
                    <span id="busuanzi_container_site_pv" style="font-size: 12px;">PV: <span id="busuanzi_value_site_pv"></span> Times</span>
                    <br>
                    Theme by <a href="https://haojen.github.io/" target="_blank" rel="noopener">Haojen Ma</a>
                </p>

            </div>
        </div>
    </div>
</footer>

<!-- jQuery -->

<script src="/myblog/js/jquery.min.js"></script>


<!-- Bootstrap Core JavaScript -->

<script src="/myblog/js/bootstrap.min.js"></script>


<!-- Custom Theme JavaScript -->

<script src="/myblog/js/blog.js"></script>


<!-- async load function -->
<script>
    function async(u, c) {
      var d = document, t = 'script',
          o = d.createElement(t),
          s = d.getElementsByTagName(t)[0];
      o.src = u;
      if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
      s.parentNode.insertBefore(o, s);
    }
</script>

<!-- jquery.tagcloud.js -->
<script>
    // only load tagcloud.js in tag.html
    if($('#tag_cloud').length !== 0){
        async("https://bigbenn.gitee.io/myblog/js/jquery.tagcloud.js",function(){
            $.fn.tagcloud.defaults = {
                //size: {start: 1, end: 1, unit: 'em'},
                color: {start: '#bbbbee', end: '#0085a1'},
            };
            $('#tag_cloud a').tagcloud();
        })
    }
</script>

<!--fastClick.js -->
<script>
    async("//cdn.bootcss.com/fastclick/1.0.6/fastclick.min.js", function(){
        var $nav = document.querySelector("nav");
        if($nav) FastClick.attach($nav);
    })
</script>



<!-- Google Analytics -->



<!-- Baidu Tongji -->


<!-- swiftype -->
<script type="text/javascript">
  (function(w,d,t,u,n,s,e){w['SwiftypeObject']=n;w[n]=w[n]||function(){
  (w[n].q=w[n].q||[]).push(arguments);};s=d.createElement(t);
  e=d.getElementsByTagName(t)[0];s.async=1;s.src=u;e.parentNode.insertBefore(s,e);
  })(window,document,'script','//s.swiftypecdn.com/install/v2/st.js','_st');

  _st('install','','2.0.0');
</script>

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<!--wechat title img-->
<img class="wechat-title-img" src="">
</body>

</html>
